package cn.lwj.webService.service.impl;

import cn.lwj.webService.base.BaseManagerImpl;
import cn.lwj.webService.base.DaoTool;
import cn.lwj.webService.entity.DataJzbView;
import cn.lwj.webService.entity.DataJzbViewList;
import cn.lwj.webService.entity.TXfRdJdfk;
import cn.lwj.webService.entity.TXfShxx;
import cn.lwj.webService.entity.TxfRdjzbYqsq;
import cn.lwj.webService.entity.TxfYqxksp;
import cn.lwj.webService.service.DrdcManager;
import cn.lwj.webService.util.ReflectUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service(value = "drdcManager")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class DrdcManagerImp extends BaseManagerImpl implements DrdcManager {
    @Resource
    private DaoTool daoTool;


    private Logger log = Logger.getLogger(this.getClass());


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public DataJzbView dataJzbSet(String jzbId, String fydm, String czType, String ywlx) throws Exception {
        DataJzbView jzbView = new DataJzbView();
        String sql = "";
        String conditon = "";
        if ("push".equals(czType)) {//推
            if ("000".equals(fydm)) {//最高院  把已经审批的同步到业务中心
                conditon = " AND (N_SPBZ=1 OR N_SPBZ=2) AND N_TBBZ=0";
            } else {//其他法院  待同步到业务中心
                conditon = " AND N_SPBZ=0 AND N_TBBZ=0 ";
            }
        } else {//拉   pull
            if ("000".equals(fydm)) {//最高院  未审批的从业务中心拉到最高院
                conditon = " AND N_SPBZ=0 AND N_TBBZ=0 ";
            } else {//其他法院  已审批的从业务中心拉到基层院
                //conditon = " AND (N_SPBZ=1 OR N_SPBZ=2) AND N_TBBZ=0";
                conditon = " AND (N_SPBZ=1 OR N_SPBZ=2) ";
            }
            //改为获取所有?
            //conditon = "";
        }
        String className = "";
        String tempDataSql = "";

        //调用反射读取对象数据
        Method[] methods = jzbView.getClass().getDeclaredMethods();
        Field[] fields = jzbView.getClass().getDeclaredFields();
        List<?> dataList = null;
        Object objNewInstance = null;
        for (Field field : fields) {
            System.out.println("field.getGenericType().toString()..." + field.getGenericType().toString());
            if (field.getGenericType().toString().equals("class java.lang.String")) continue;
            if (field.getGenericType().toString().equals("java.util.Map<java.lang.String, java.lang.Class>")) continue;
            if (field.getGenericType().toString().equals("java.util.Map<java.lang.String, java.lang.Object>")) continue;
            if (field.getGenericType().toString().equals("java.util.Map<java.lang.String, java.lang.Class<?>>")) continue;
            //下级法院拉取时不拉阶段反馈的  最高院推送时不拉取阶段反馈的
            if ("jdfkList".equals(field.getName()) && ((!"000".equals(fydm) && "pull".equals(czType)) || ("000".equals(fydm) && "push".equals(czType)))) {
                continue;
            }
            if (ywlx != null && !"".equals(ywlx)) {
                //下级法院拉取时不拉阶段反馈的  最高院推送时不拉取阶段反馈的
                if (DataJzbView.JDFK.equals(ywlx) && !"jdfkList".equals(field.getName())) {
                    continue;
                } else if (DataJzbView.YQXK.equals(ywlx) && !"yqxksp".equals(field.getName())) {
                    continue;
                } else if (DataJzbView.YQSQ.equals(ywlx) && !"yqsq".equals(field.getName())) {
                    continue;
                } else if (DataJzbView.SHBL.equals(ywlx) && !"shxx".equals(field.getName())) {
                    continue;
                }
            }
            className = field.getGenericType().toString().replaceFirst("class ", "");
            //泛型判断
            if (className.contains("<") && className.contains(">")) {
                className = className.substring(0, className.indexOf("<"));
                if (className.equals("java.util.List")) {
                    className = "java.util.ArrayList";
                }
            }
            objNewInstance = Class.forName(className).newInstance();
            if (objNewInstance instanceof List) {
                dataList = (List<?>) objNewInstance;
                if (DataJzbView.dataType.get(field.getName()) == null || DataJzbView.dataSql.get(field.getName()).toString() == null) {
                    continue;
                }
                tempDataSql = DataJzbView.dataSql.get(field.getName()) == null ? "" : DataJzbView.dataSql.get(field.getName()).toString();
                if (tempDataSql.trim().equals("")) continue;
                //参数替换
                tempDataSql = tempDataSql.replace("@JZBID", jzbId).replace("@CONDITION", conditon);
                dataList = findListByNoCurrentThread(DataJzbView.dataType.get(field.getName()), tempDataSql);
                if (dataList != null && dataList.size() > 0) {
                    ReflectUtils.setFieldValue(jzbView, field.getName(), dataList);
                }

            } else {
                tempDataSql = getStr(DataJzbView.dataSql.get(field.getName()));
                if (DataJzbView.dataType.get(field.getName()) == null || "".equals(tempDataSql) || "".equals(tempDataSql.trim())) {
                    continue;
                }
                //参数替换
                tempDataSql = tempDataSql.replace("@JZBID", jzbId).replace("@CONDITION", conditon);
                //TxfYqxksp单独处理
                if (field.getGenericType().toString().equals("class cn.net.withub.common.model.TxfYqxksp")) {
                    sql = "SELECT * FROM `t_xf_yqxksp` WHERE " + tempDataSql;
                    List<TxfYqxksp> yqxksps = daoTool.findSQLList(sql, TxfYqxksp.class, 1, 10, null);
                    if (yqxksps != null && yqxksps.size() > 0) {
                        yqxksps.get(0).setCurrentJzbId(jzbId);
                        yqxksps.get(0).setcJzbIds(getYqxkGlaj(yqxksps.get(0).getcYqxkId()));
                        jzbView.setYqxksp(yqxksps.get(0));
                    }
                    continue;
                }
                objNewInstance = (Object) loadByNoCurrentThread(DataJzbView.dataType.get(field.getName()), tempDataSql);
                log.info("objNewInstance data..." + objNewInstance);
                if (objNewInstance != null) {
                    System.out.println("setObj..." + objNewInstance.getClass());
                    ReflectUtils.setFieldValue(jzbView, field.getName(), objNewInstance);
                }
            }
        }
        jzbView.setYwlx(ywlx);
        return jzbView;
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public DataJzbViewList dataJzbListSet(String fydm, String czType) throws Exception {
        DataJzbViewList jzbViewList = new DataJzbViewList();
        int pageSize = 30;
        String condition = "";
        if ("pull".equals(czType)) {//拉取
            if ("000".equals(fydm)) {
                condition = " AND N_SPBZ=0 AND N_TBBZ=0";
            } else {
                condition = " AND (N_SPBZ=1 OR N_SPBZ=2) AND N_TBBZ<>2";
            }
        }else {//推送
            if ("000".equals(fydm)) {
                condition = " AND (N_SPBZ=1 OR N_SPBZ=2) AND N_TBBZ=0";
            } else {
                condition = " AND N_SPBZ=0 AND N_TBBZ=0";
            }
        }
        String sql = "";
        if (("000".equals(fydm) && "pull".equals(czType)) || (!"000".equals(fydm) && "push".equals(czType))) {//最高院才拉取阶段反馈  下级法院推送阶段反馈
            //sql = "SELECT * FROM t_xf_rd_jdfk WHERE N_TBBZ=0 ";
            sql = "SELECT * FROM `t_xf_rd_jdfk` WHERE N_TBBZ=0 ";
            List<TXfRdJdfk> jdfks = daoTool.findSQLList(sql, TXfRdJdfk.class, 1, 50, null);
            if (jdfks != null && jdfks.size() > 0) jzbViewList.setJdfks(jdfks);
        }
        sql = "SELECT * FROM t_xf_yqxksp WHERE 1=1 " + condition;
        List<TxfYqxksp> yqxksps = daoTool.findSQLList(sql, TxfYqxksp.class, 1, pageSize, null);
        if (yqxksps != null && yqxksps.size() > 0) {
            for (TxfYqxksp yqsp : yqxksps) {
                yqsp.setcJzbIds(getYqxkGlaj(yqsp.getcYqxkId()));
            }
        }
        sql = "SELECT * FROM t_xf_rdjzb_yqsq WHERE 1=1 " + condition;
        List<TxfRdjzbYqsq> yqsqs = daoTool.findSQLList(sql, TxfRdjzbYqsq.class, 1, pageSize, null);
        sql = "SELECT * FROM t_xf_shxx WHERE 1=1 " + condition;
        List<TXfShxx> shxxes = daoTool.findSQLList(sql, TXfShxx.class, 1, pageSize, null);
        if (yqxksps != null && yqxksps.size() > 0) jzbViewList.setYqxksps(yqxksps);
        if (yqsqs != null && yqsqs.size() > 0) jzbViewList.setYqsqs(yqsqs);
        if (shxxes != null && shxxes.size() > 0) jzbViewList.setShxxes(shxxes);
        return jzbViewList;
    }


}
